﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Xin478.CSWork.Common;

namespace Xin478.CSWork.Client
{
    public partial class FileForm : MyForm
    {
        static int sendNum = 0;
        static object lockHelper = new object();
        TransferFileHelper tranHelper;

        public FileForm()
        {
            InitializeComponent();
            this.Icon = MainFormResource.Conntected;
            this.Closing+=new CancelEventHandler(FileForm_Closing);

            imageList1.Images.Add(MainFormResource.上传);
            imageList1.Images.Add(MainFormResource.下载);
            imageList1.Images.Add(MainFormResource.上传未启动);
            imageList1.Images.Add(MainFormResource.下载未启动);
            
            this.listView1.OwnerDraw = true;
            this.listView1.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);
            this.listView1.DrawSubItem += new DrawListViewSubItemEventHandler(listView1_DrawSubItem);
        }

        public TransferFileHelper TransferFileHelper {
            get { return this.tranHelper; }
            set
            {
                if (value == null) throw new ArgumentNullException();
                this.tranHelper = value;

                this.tranHelper.AddTransferHandler += FileForm_AddItem;
                this.tranHelper.StartTransferHandler += FileForm_StartItem;
                this.tranHelper.TransferBlockHandler += FileForm_ProgressChange;
                this.tranHelper.FinishTransferHandler += FileForm_FinishItem;
            }
        }

        void FileForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.tranHelper.AddTransferHandler -= FileForm_AddItem;
            this.tranHelper.StartTransferHandler -= FileForm_StartItem;
            this.tranHelper.TransferBlockHandler -= FileForm_ProgressChange;
            this.tranHelper.FinishTransferHandler -= FileForm_FinishItem;
            e.Cancel = false;
            this.Dispose();
        }

        void FileForm_ProgressChange(TransferFileInfo tfileInfo)
        {
            WinForm.DoThreadSafe(this.listView1, delegate()
            {
                foreach(ListViewItem item in this.listView1.Items.Find(tfileInfo.Guid, false))
                {
                    Rectangle rect = item.SubItems[1].Bounds;
                    TransferFileInfo fi = item.Tag as TransferFileInfo;
                    Graphics g = Graphics.FromHwnd(listView1.Handle);
                    if (fi.IsFail)
                    {
                        DrawProgress(rect, 1, fi.FileName, Color.PeachPuff, Color.Black, g);
                    }
                    else
                    {
                        float p = (float)fi.TransferSize / (float)fi.FileSize;
                        if (fi.Status == TransferFileStatus.DownLoad)
                            DrawProgress(rect, p, fi.FileName, Color.LightGreen, Color.Black, g);
                        else
                            DrawProgress(rect, p, fi.FileName, Color.LightBlue, Color.Black, g);
                    }
                }
            });
        }

        void FileForm_AddItem(TransferFileInfo tfileInfo)
        {
            ListViewItem item = new ListViewItem();
            item.Name = tfileInfo.Guid;
            item.SubItems.AddRange(new string[] { "a", "a" });
            item.Tag=tfileInfo;

            switch (tfileInfo.Status)
            {
                case TransferFileStatus.DownLoad:
                    //item.BackColor = Color.YellowGreen;
                    item.ImageIndex = 3;
                    break;
                case TransferFileStatus.UpLoad:
                    //item.BackColor = Color.SkyBlue;
                    item.ImageIndex = 2;
                    break;
            }
            WinForm.DoThreadSafe(this.listView1, delegate()
            {
                this.listView1.Items.Add(item);
            });
        }

        void FileForm_StartItem(TransferFileInfo tfileInfo)
        {
            if (tfileInfo.Status == TransferFileStatus.DownLoad)
            {
                tfileInfo.TempFileDir = UserSession.Current.ReceiveFilePath;
                tfileInfo.FileDir = UserSession.Current.ReceiveFilePath;
                tfileInfo.ToFileName = tfileInfo.MD5 + tfileInfo.FileExt;
            }
            WinForm.DoThreadSafe(this.listView1, delegate()
            {
                foreach (ListViewItem item in this.listView1.Items.Find(tfileInfo.Guid, false))
                {
                    TransferFileInfo tfile = item.Tag as TransferFileInfo;
                    switch (tfile.Status)
                    {
                        case TransferFileStatus.DownLoad:
                            //item.BackColor = Color.YellowGreen;
                            item.ImageIndex = 1;
                            break;
                        case TransferFileStatus.UpLoad:
                            //item.BackColor = Color.SkyBlue;
                            item.ImageIndex = 0;
                            break;
                    }
                }
            });
        }

        void FileForm_FinishItem(TransferFileInfo tfileInfo)
        {
            WinForm.DoThreadSafe(this.listView1, delegate()
            {
                foreach (ListViewItem item in this.listView1.Items.Find(tfileInfo.Guid, false))
                {
                    this.listView1.Items.Remove(item);
                }
            });
            lock (lockHelper)
            {
                toolStripStatusLabel2.Text = (++sendNum).ToString();
            }
        }

        public void StartFinishItem(TransferFileInfo tfileInfo)
        {
            tfileInfo.FileName = tfileInfo.MD5;
        }

        //绘制进度条列的subitem 
        private void DrawProgress(Rectangle rect, float percent, string text, Color bgColor, Color fontColor, Graphics g)
        {
            if (rect.Height > 2 && rect.Width > 2)
            {                
                int width = (int)(rect.Width * percent);
                Rectangle newRect = new Rectangle(rect.Left + 1, rect.Top , width - 2, rect.Height );
                using (Brush tmpb = new SolidBrush(bgColor))
                {
                    g.FillRectangle(tmpb, newRect);
                }
               // newRect = new Rectangle(rect.Left + 1, rect.Top + 1, rect.Width - 2, rect.Height - 2);
               // g.DrawRectangle(Pens.RoyalBlue, newRect);
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Near;
                sf.LineAlignment = StringAlignment.Center;
                sf.Trimming = StringTrimming.EllipsisCharacter;
                newRect = new Rectangle(rect.Left + 1, rect.Top + 1, rect.Width - 2, rect.Height - 2);

                using (Brush b = new SolidBrush(fontColor))
                {
                    g.DrawString(text, this.Font, b, newRect, sf);
                }
            }
            else
            {
                return;
            }
        }

        void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawDefault = true;
        }

        void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            if (e.ColumnIndex != 1 ) 
            {
                e.DrawDefault = true;
            }
            else
            {
                TransferFileInfo fi = e.Item.Tag as TransferFileInfo;
                Rectangle rect = new Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
                
                if (fi.IsFail)
                {
                    DrawProgress(rect, 1, fi.FileName, Color.PeachPuff, Color.Black, e.Graphics);
                }
                else
                {
                    float p = (float)fi.TransferSize / (float)fi.FileSize;
                    if(fi.Status==TransferFileStatus.DownLoad)
                        DrawProgress(rect, p, fi.FileName, Color.LightGreen, Color.Black, e.Graphics);
                    else
                        DrawProgress(rect, p, fi.FileName, Color.LightBlue, Color.Black, e.Graphics);
                }
            }
        }

    }
}
